package org.tema.ejb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.tema.entities.Report;
import org.tema.entities.TestPlan;

/**
 * Session Bean implementation class ReportBean
 */
@Stateless
public class ReportBean implements ReportBeanLocal {

    /**
     * Default constructor. 
     */
    public ReportBean() {
        // TODO Auto-generated constructor stub
    }

    @PersistenceContext
	private EntityManager entityManager;
	
	@EJB
	ProductBeanLocal productBean;
	
	@EJB
	UserBeanLocal userBean;
	
	@EJB
	TestSpecBeanLocal specificationBean;
	
	@EJB
	TestPlanBeanLocal planBean;
  
 //Bean CRUD methods
	
	//report creation is implemented in the TestPlanBean

	
	public void deleteReport(Report report){
		entityManager.remove(entityManager.merge(report));
		
	}

	@SuppressWarnings("unchecked")
	public Collection <Report> findAllReports(){
		Query query = entityManager.createQuery(" SELECT c FROM Report c ");
		return (List <Report>) query.getResultList();
	}
	
	
	@SuppressWarnings("unchecked")
	public Report findReportById( int reportId){
		Query query = entityManager.createQuery(" SELECT c FROM Report c WHERE c.id = :id ");
		query.setParameter("id", reportId);
		return (Report) query.getSingleResult();
	}
	
	@SuppressWarnings("unchecked")
	public Collection <Report> findReportsByTestPlan(int testPlanId){
		Query query = entityManager.createQuery(" SELECT c FROM Report c WHERE c.plan.id = :id");
		query.setParameter("id", testPlanId);
		return (Collection <Report>) query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public Collection <Report> findReportsByTestPlanSummary(String planSummary){
		TestPlan plan = planBean.findTestPlanBySummary(planSummary);
		int plan_id = plan.getId();
		return this.findReportsByTestPlan(plan_id);
	}
	
	@SuppressWarnings("unchecked")
	public Collection <Report> findReportsByProductId(int productId){
		Collection <TestPlan> testPlan = this.planBean.findTestPlanByProductId(productId);
		Collection <Report> reportsList = new HashSet <Report> ();
		Collection <Report> allReportsList =  new HashSet <Report> ();
		for(TestPlan tp1: testPlan){
			 
			for(Report r: this.findReportsByTestPlan( tp1.getId() )){
				allReportsList.add(r);
			}
		}
		return allReportsList;
	}
	
	@SuppressWarnings("unchecked")
	public Collection <Report> findReportsByProductSummary(String productSummary){
		Collection <TestPlan> testPlan = this.planBean.findTestPlanByProductName(productSummary);
		Collection <Report> reportsList = new ArrayList <Report> ();
		Collection <Report> allReportsList =  new ArrayList <Report> ();
		for(TestPlan tp1: testPlan){
			reportsList = this.findReportsByTestPlan( tp1.getId() );
			for(Report r: reportsList){
				allReportsList.add(r);
			}
		}
		return allReportsList;
	}
}
